package com.rykj.energy.task.manage.actuator.impl;

import com.alibaba.fastjson.JSON;
import com.rykj.energy.common.dto.TaskCameraRelationDto;
import com.rykj.energy.common.utils.CollectionUtils;
import com.rykj.energy.common.utils.HttpClientUtils;
import com.rykj.energy.common.utils.JsonUtil;
import com.rykj.energy.database.mapper.CameraMapper;
import com.rykj.energy.task.manage.actuator.ITaskManage;
import com.rykj.energy.task.manage.vo.TaskInfoDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class VideoTaskManage implements ITaskManage {

    public static String startTask = "/scheduleservice/task/start";

    public static String stopTask = "/scheduleservice/task/stop";

    public static String updateTask = "/scheduleservice/task/update";

    @Autowired
    private CameraMapper cameraMapper;

    @Value("${rykj.scheduler.http:http://127.0.0.1:8186}")
    private String schedulerHttp;

    @Override
    public boolean startTask(List<TaskCameraRelationDto> voList) throws Exception {

        if(CollectionUtils.isEmpty(voList)){
            throw new Exception("需要开启的任务不能为空");
        }
        List<TaskInfoDto> infoDtoList = new ArrayList<>();
        for(TaskCameraRelationDto taskCameraRelationDto : voList){
            TaskInfoDto taskInfoDto = new TaskInfoDto();
            taskInfoDto.setCamera_id(String.valueOf(taskCameraRelationDto.getCameraId()));
            taskInfoDto.setFlow_id(String.valueOf(taskCameraRelationDto.getTaskId()));
            taskInfoDto.setTask_code(taskCameraRelationDto.getTaskCode());
            taskInfoDto.setPriority(taskCameraRelationDto.getLevel());
            taskInfoDto.setTask_content(taskCameraRelationDto.getBusinessContent());
            taskInfoDto.setOrg_id(taskCameraRelationDto.getOrgId());
            taskInfoDto.setBasicCamera(cameraMapper.selectById(taskCameraRelationDto.getCameraId()));

            //TODO 暂时为空
            taskInfoDto.setTask_content(JSON.toJSONString(new HashMap<String,Object>()));

            infoDtoList.add(taskInfoDto);
        }
        String startUrl = schedulerHttp + startTask;
        try {
            HttpClientUtils.post(startUrl, JsonUtil.beanToString(infoDtoList));
        }catch (Exception e){
            log.error("任务启动异常，请检查!!",e);
//            throw new Exception("任务启动异常，请检查!!");
        }
        log.info("任务启动成功,请求{},参数：{}",startUrl,JsonUtil.beanToString(infoDtoList));
        return true;
    }

    @Override
    public boolean stopTask(List<TaskCameraRelationDto> voList) throws Exception {

        if(CollectionUtils.isEmpty(voList)){
            throw new Exception("需要停止的任务不能为空");
        }
        List<TaskInfoDto> infoDtoList = new ArrayList<>();
        for(TaskCameraRelationDto taskCameraRelationDto : voList){
            TaskInfoDto taskInfoDto = new TaskInfoDto();
            taskInfoDto.setCamera_id(String.valueOf(taskCameraRelationDto.getCameraId()));
            taskInfoDto.setFlow_id(String.valueOf(taskCameraRelationDto.getTaskId()));
            taskInfoDto.setTask_code(taskCameraRelationDto.getTaskCode());
            taskInfoDto.setPriority(taskCameraRelationDto.getLevel());
            taskInfoDto.setTask_content(taskCameraRelationDto.getBusinessContent());
            taskInfoDto.setOrg_id(taskCameraRelationDto.getOrgId());
            taskInfoDto.setBasicCamera(cameraMapper.selectById(taskCameraRelationDto.getCameraId()));

            //TODO 暂时为空
            taskInfoDto.setTask_content(JSON.toJSONString(new HashMap<String,Object>()));

            infoDtoList.add(taskInfoDto);
        }

        String stopUrl = schedulerHttp + stopTask;
        try {
            HttpClientUtils.post(stopUrl, JsonUtil.beanToString(infoDtoList));
        }catch (Exception e){
            log.error("任务停止异常，请检查!!",e);
//            throw new Exception("任务停止异常，请检查!!");
        }
        log.info("任务停止成功,请求{},参数：{}",stopTask,JsonUtil.beanToString(infoDtoList));
        return true;
    }

    @Override
    public boolean updateTask(List<TaskCameraRelationDto> voList) throws Exception {

        if(CollectionUtils.isEmpty(voList)){
            throw new Exception("需要开启的任务不能为空");
        }
        List<TaskInfoDto> infoDtoList = new ArrayList<>();
        for(TaskCameraRelationDto taskCameraRelationDto : voList){
            TaskInfoDto taskInfoDto = new TaskInfoDto();
            taskInfoDto.setCamera_id(String.valueOf(taskCameraRelationDto.getCameraId()));
            taskInfoDto.setFlow_id(String.valueOf(taskCameraRelationDto.getTaskId()));
            taskInfoDto.setTask_code(taskCameraRelationDto.getTaskCode());
            taskInfoDto.setPriority(taskCameraRelationDto.getLevel());
            taskInfoDto.setTask_content(taskCameraRelationDto.getBusinessContent());
            taskInfoDto.setOrg_id(taskCameraRelationDto.getOrgId());
            taskInfoDto.setBasicCamera(cameraMapper.selectById(taskCameraRelationDto.getCameraId()));

            //TODO 暂时为空
            taskInfoDto.setTask_content(JSON.toJSONString(new HashMap<String,Object>()));

            infoDtoList.add(taskInfoDto);
        }

        String updateUrl = schedulerHttp + updateTask;
        try {
            HttpClientUtils.post(updateUrl, JsonUtil.beanToString(infoDtoList));
        }catch (Exception e){
            log.error("任务更新异常，请检查!!",e);
//            throw new Exception("任务更新异常，请检查!!");
        }

        log.info("任务更新成功,请求url:{},参数：{}",updateTask,JsonUtil.beanToString(infoDtoList));
        return true;
    }
}
